/*
https://leetcode-cn.com/problems/count-special-quadruplets/solution/tong-ji-te-shu-si-yuan-zu-by-leetcode-so-50e2/
 */
public class Solution1995 {
    public int countQuadruplets(int[] nums) {
        int n=nums.length;
        int ans=0;
        int[] rec=new int[105];
        for (int b=n-3;b>=1;b--){
            for (int d=b+2;d<n;d++){
                if (nums[d]-nums[b+1]>0){
                    rec[nums[d]-nums[b+1]]++;
                }
            }
            for (int a=0;a<b;a++){
                if (nums[a]+nums[b]<=100) {
                    ans+=rec[nums[a]+nums[b]];
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1995().countQuadruplets(new int[]{28,8,49,85,37,90,20,8}));
    }
}
